package com.li.puzzle.viewpagerindicator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/11/24.
 */

public class ViewPagerIndicator extends LinearLayout {

    private int mSingleWidth;
    private int mSingleHeight;
    private int mTriangleWidth;
    private int mTriangleHeight;
    private Paint mPaint;
    private Path mPath;
    private List<String> mTitles;
    private ViewPager mViewPager;
    private Canvas mCanvas;
    private PointF movePoint;

    public ViewPagerIndicator(Context context) {
        this(context, null);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void setViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("tag",position+"  "+positionOffset);
                movePoint.set(positionOffset * mSingleWidth + position * mSingleWidth+mSingleWidth / 2 - mTriangleWidth / 2
                        , mSingleHeight);
                invalidate();

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setTitles(List<String> titles) {
        mTitles = titles;
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPath = new Path();
        movePoint=new PointF();

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(movePoint.x,movePoint.y);
        mPath.moveTo(0, 0);
        mPath.lineTo(mTriangleWidth, 0);
        mPath.lineTo(mTriangleWidth / 2, -mTriangleHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
        super.dispatchDraw(canvas);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        setBackgroundColor(Color.GRAY);
        if (mTitles == null) {
            return;
        }
        mSingleWidth = w / mTitles.size();
        mSingleHeight = h;

        mTriangleWidth = mSingleWidth / 6;
        mTriangleHeight = mTriangleWidth / 2;
        addTitles();
    }

    private void addTitles() {
        for (int i = 0; i < mTitles.size(); i++) {
            final int j=i;
            String title=mTitles.get(i);
            TextView textView = new TextView(getContext());
            textView.setText(title);
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            params.width = mSingleWidth;
            params.height = mSingleHeight;
            textView.setLayoutParams(params);
            textView.setTextColor(Color.WHITE);
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(j,false);
                }
            });
            addView(textView);
        }

    }

}
